package com.wzqj.agent.web.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.util.Arrays;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.wzqj.wsp.response.ResponseValue;
import com.wzqj.wsp.upload.OssTemplate;


@Controller
@RequestMapping("/file")
public class FileUploadController {
	//Logger log = Logger.getLogger(PublicController.class);
	
    private OssTemplate ossTemplate = new OssTemplate();

    private static String EXT_NAME = "gif,jpg,jpeg,png,bmp,zip,apk"; //扩展名
    private static int FILE_MAX_SIZE = 1024*500;//最大上传图片大小
    /**
     * 取文件的扩展名
     *
     * @param fileName
     * @return
     */
    public static String getExtensionName(String fileName) {
        if (fileName == null || fileName.trim().length() == 0) {
            return null;
        }
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    /**
     * 图片上传
     * @param req
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/upload")
    public JSONObject upload(MultipartHttpServletRequest req) throws Exception {
    	ResponseValue rv = new ResponseValue();
    	//对应AjaxUpload中的name
        MultipartFile mf = req.getFile("myfile");
        String filename = mf.getOriginalFilename();
        byte[] buf = mf.getBytes();
        int fileSize = buf.length;
        //log.info("file upload length:" + fileSize);
        if (fileSize > FILE_MAX_SIZE) {
			rv.setErrMsg("文件大小不能超过500k");
			return JSONObject.parseObject(rv.toString());
		}
        //调用方法取文件的扩展名
        String fileExt = getExtensionName(filename);
        if (buf == null) {
        	rv.setErrMsg("文件为空");
        	return JSONObject.parseObject(rv.toString());
        }
        else if(!Arrays.<String>asList(EXT_NAME.split(",")).contains(fileExt.toLowerCase())){
        	rv.setErrMsg("只能上传指定格式的文件");
        	return JSONObject.parseObject(rv.toString());
        }
        else if((fileExt.equals("jpg") || fileExt.equals("gif") || fileExt.equals("png")) && req.getParameter("width")!=null && req.getParameter("height")!=null){
            int widthlimit = Integer.valueOf(req.getParameter("width"));
            int heightlimit = Integer.valueOf(req.getParameter("height"));
            
            BufferedImage bi = ImageIO.read(new ByteArrayInputStream(buf));
            int width = bi.getWidth();
            int height = bi.getHeight();
            if (widthlimit < width || heightlimit < height) {
            	rv.setErrMsg("只能上传" + widthlimit + "*" + heightlimit + "以内大小的图片");
            	return JSONObject.parseObject(rv.toString());
            }else{
                String resId = ossTemplate.putFile(filename,buf); //上传到资源服务器
                //log.info("上传成功：" + resId);
                rv.setData(resId);
                return JSONObject.parseObject(rv.toString());
            }
        }
        else {
            String resId = ossTemplate.putFile(filename, buf); //上传到资源服务器
            //log.info("上传成功：" + resId);
            rv.setData(resId);
            return JSONObject.parseObject(rv.toString());
        }
    }
}
